package com.test.demo.controller;

import com.google.gson.Gson;
import com.test.demo.po.Answer;
import com.test.demo.po.Question;
import com.test.demo.service.AnswerService;
import com.test.demo.service.QAService;
import com.test.demo.service.QuestionService;
import com.test.demo.service.impl.QAServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Cenoo on 2017/7/3.
 */

@Controller
@RequestMapping(value = "/QA")
public class QAController {
//    @Autowired
//    private QuestionService questionService;
//
//    @Autowired
//    private AnswerService answerService;

    @Autowired
    private QAService qaService;

    @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
    @ResponseBody
    public Object addQuestion(@RequestBody String json) {
        Gson gson = new Gson();
        Question question = gson.fromJson(json, Question.class);
        Date date = new Date();
        question.setAskTime(date.getTime());
        question.setLastTime(date.getTime());
        question.setReplyCount(0);
        try {
            int result = qaService.addQuestion(question);
            if (result == 1) {
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public Object search(HttpServletRequest request) {
        String keyword = request.getParameter("keyword");
        String tag = request.getParameter("tag");
        Gson gson = new Gson();
        try {
            List<Question> result = null;
            if (keyword == null)
                result = qaService.searchByTag(tag);
            else
                result = qaService.searchQuestion(keyword);
            if (result != null) {
                String json = gson.toJson(result);
                return json;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping(value = "/fetchAllQuestion", method = RequestMethod.GET)
    @ResponseBody
    public Object fetchAll(HttpServletRequest request) {
        Gson gson = new Gson();
        try {
            List<Question> questions = qaService.getAllQuestion();
            if (questions != null) {
                return gson.toJson(questions);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    @RequestMapping(value = "/fetchAnswer", method = RequestMethod.GET)
    @ResponseBody
    public Object fetchAnswer(HttpServletRequest request) {
        Gson gson = new Gson();
        String s = request.getParameter("question");
        if (s != null) {
            int questionId = Integer.parseInt(s);
            List<Answer> result = null;
            try {
                result = qaService.getAnswersOfQuestion(questionId);
                if (result != null)
                    return gson.toJson(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        } else return "wrong arguments";
    }

    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ResponseBody
    public Object insertReply(@RequestBody String json) {
        Gson gson = new Gson();
        Answer answer = gson.fromJson(json, Answer.class);
        try {
            int result = qaService.addAnswer(answer);
            if (result == 2 )
                return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }
}

